
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-12-08     Wangjiangbo       the first version
 */

#include "app_comm.h"
#include "cJSON.h"
#include "app_io.h"
#include "app_arm_control.h"
//#include "app_force_move.h"
#include "app_dctft.h"
#include "DCTft/DCMasterDevice.h"
#define DBG_TAG "comm"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>


typedef struct {
    uint16_t cmd;

    uint8_t fan;

    uint8_t locked;   //台车锁定

    uint8_t beep_st;
    uint8_t beep_freq;

    char* opt;
    char* opt_arg;

} json_rep;

typedef struct {
    uint16_t cmd;

    uint16_t error;

    uint8_t key_start;
    uint8_t key_drag;

    uint8_t emg_surgical;
    uint8_t emg_doctor;

    uint8_t locked;//台车锁定

    int32_t fx;
    int32_t fy;
    int32_t fz;
    int32_t fr;

    int8_t u_d;
    int8_t l_r;
    int8_t i_o;

    int8_t pitch;
    int8_t yaw;
    int8_t roll;

    int8_t uf_st;

    char* opt;
    char* opt_arg;

} json_req;

static bool _req_to_jsong(json_req* req, char* json_string, int max_len)
{
    cJSON *cmd = cJSON_CreateNumber(req->cmd);
    cJSON *err = cJSON_CreateNumber(req->error);
    cJSON *drag = cJSON_CreateNumber(req->key_drag);
    cJSON *start = cJSON_CreateNumber(req->key_start);

    cJSON *key = cJSON_CreateObject();
    cJSON_AddItemToObject(key, "drag", drag);
    cJSON_AddItemToObject(key, "start", start);

    cJSON *surgical = cJSON_CreateNumber(req->emg_surgical);
    cJSON *doctor = cJSON_CreateNumber(req->emg_doctor);
    cJSON *emg = cJSON_CreateObject();
    cJSON_AddItemToObject(emg, "surgical", surgical);
    cJSON_AddItemToObject(emg, "doctor", doctor);

    cJSON *locked = cJSON_CreateNumber(req->locked);//台车锁定

    cJSON *fx = cJSON_CreateNumber(req->fx);
    cJSON *fy = cJSON_CreateNumber(req->fy);
    cJSON *fz = cJSON_CreateNumber(req->fz);
    cJSON *fr = cJSON_CreateNumber(req->fr);

    cJSON *force = cJSON_CreateObject();
    cJSON_AddItemToObject(force, "x", fx);
    cJSON_AddItemToObject(force, "y", fy);
    cJSON_AddItemToObject(force, "z", fz);
    cJSON_AddItemToObject(force, "r", fr);


    cJSON *_u_d = cJSON_CreateNumber(req->u_d);
    cJSON *_l_r = cJSON_CreateNumber(req->l_r);
    cJSON *_i_o = cJSON_CreateNumber(req->i_o);
    cJSON *_pitch = cJSON_CreateNumber(req->pitch);
    cJSON *_yaw = cJSON_CreateNumber(req->yaw);
    cJSON *_roll = cJSON_CreateNumber(req->roll);
    cJSON *_uf_st = cJSON_CreateNumber(req->uf_st);

    cJSON *direct = cJSON_CreateObject();
    cJSON_AddItemToObject(direct, "ud", _u_d);
    cJSON_AddItemToObject(direct, "lr", _l_r);
    cJSON_AddItemToObject(direct, "io", _i_o);
    cJSON_AddItemToObject(direct, "pitch", _pitch);
    cJSON_AddItemToObject(direct, "yaw", _yaw);
    cJSON_AddItemToObject(direct, "roll", _roll);
    cJSON_AddItemToObject(direct, "ufst", _uf_st);

    cJSON *opt = cJSON_CreateString(req->opt);
    cJSON *opt_arg = cJSON_CreateString(req->opt_arg);

    cJSON *root = cJSON_CreateObject();
    cJSON_AddItemToObject(root, "cmd", cmd);
    cJSON_AddItemToObject(root, "err", err);
    cJSON_AddItemToObject(root, "emg", emg);
    cJSON_AddItemToObject(root, "key", key);
    cJSON_AddItemToObject(root, "locked", locked);//台车锁定
    cJSON_AddItemToObject(root, "force", force);
    cJSON_AddItemToObject(root, "direct", direct);
    cJSON_AddItemToObject(root, "opt", opt);
    cJSON_AddItemToObject(root, "opt_arg", opt_arg);

    cJSON_PrintPreallocated(root, json_string, max_len, 1);

    LOG_D("json_rep = \n%s", json_string);

    cJSON_Delete(root);
    return true;
}
//请求发送处理
void app_req_process(char* json_req_buff, int json_req_buff_max_len)
{
    json_req req = {0};
    iomap io;
    arm_direct _direct;
    uint32_t axit[6] = {0};

    app_io_get_gpio_map(&io);
//    app_force_get_axit(axit);
    app_get_arm_direct(&_direct);

    req.cmd = 0;
    req.emg_surgical = io.emg_st[0];
    req.emg_doctor = io.emg_st[1];
    req.key_start = io.key_st[e_key_start];
    req.key_drag = io.key_st[e_key_drag];

    req.locked = pallet_locked;

    req.fx = axit[0];
    req.fy = axit[1];
    req.fz = axit[2];
    req.fr = axit[3];

    req.u_d = _direct.u_d;
    req.l_r = _direct.l_r;
    req.i_o = _direct.i_o;
    req.pitch = _direct.pitch;
    req.yaw = _direct.yaw;
    req.roll = _direct.roll;
    req.uf_st = _direct.uf_st;
    LOG_D("dir:%d, %d, %d,%d, %d, %d\r\n",req.u_d,req.l_r,req.i_o,req.pitch,req.yaw,req.roll,req.uf_st);
    req.opt = "login";
    req.opt_arg = "user=factory,pwd=123456";

    _req_to_jsong(&req, json_req_buff, json_req_buff_max_len);

}

static bool _json_to_req(char* json_string, json_rep* json_rep)
{
    int len = strlen(json_string);

    if(len > 1024 || json_string == NULL)
        return false;

    cJSON *root = cJSON_Parse(json_string);
    cJSON *cmd = cJSON_GetObjectItem(root, "cmd");
    cJSON *fan = cJSON_GetObjectItem(root, "fan");
    cJSON *locked = cJSON_GetObjectItem(root, "locked");
    cJSON *beep_st = cJSON_GetObjectItem(root, "beep");
    cJSON *beep_freq = cJSON_GetObjectItem(root, "beep_freq");
    cJSON *opt = cJSON_GetObjectItem(root, "opt");
    cJSON *opt_arg = cJSON_GetObjectItem(root, "opt_arg");

    json_rep->fan = cJSON_GetNumberValue(fan);
    json_rep->locked = cJSON_GetNumberValue(locked);
    json_rep->beep_st = cJSON_GetNumberValue(beep_st);
    json_rep->beep_freq = cJSON_GetNumberValue(beep_freq);
    json_rep->opt = cJSON_GetStringValue(opt);
    json_rep->opt_arg = cJSON_GetStringValue(opt_arg);

    LOG_D("json_rep = \n%s", cJSON_Print(root));

    cJSON_Delete(root);
    return true;
}

void app_comm_process(char* json_req_string, char* json_rep_buff, int json_rep_buff_max_len)
{
    json_rep rep = {0};
    _json_to_req(json_req_string, &rep);
    dctft_widget_locked_set(rep.locked);

}

#if 0


[D/comm] json_req =
{
    "cmd":  1,                          //命令
    "fan":  1,                          //风扇
    "rgbs": [                           //rgb灯 一共两个
        {
            "style": 1,                 //0x4 红灯 0x2绿灯 0x1蓝灯  0x7 rgb全亮白等
            "freq": 2                   //闪烁评率 0 = 不闪 100 = 100hz闪烁
        },
        {
            "style": 3,
            "freq": 4
        }
    ]
}
[D/comm] json_rep =
{
    "cmd":  32769,                      //命令返回 1 | 0x8000
    "hall": {                           //霍尔传感器
        "arm":  0,                      //器械结合
        "shell": 0,                     //无菌罩结合
        "trocar": 0                     //戳卡结合
    },
    "key":  {
        "drag": 0,                      //动力盒 拖动按键
        "err_out":      0,              //动力盒 故障退出按键
        "drag_rcm_local": 0,            //RCM臂  位置拖动
        "drag_rcm_attitude":0,          //RCM臂  姿态拖动
        "en":   0,                      //RCM臂拖动备选方案 使用按键调整位置 使能
        "up":   0,                      //RCM臂拖动备选方案  上
        "down": 0,                      //RCM臂拖动备选方案  下
        "left": 0,                      //RCM臂拖动备选方案  左
        "right": 0                      //RCM臂拖动备选方案  右
    },
    "sensor":       {
        "top":  0,                      //RCM限位传感器 上
        "bottom": 0,                    //RCM限位传感器 下
        "left": 0,                      //RCM限位传感器 左
        "right": 0                      //RCM限位传感器 右
    }
}


#endif

